<?php
class BConfig
{
    /**
     * 配置信息
     *
     * @access private
     * @var array
     */
    private $_supports = array(

        'localhost' => array(
            'debug'=> true, //是否调试模式
            'database'=> array(  //数据库配置
                'default'=> 'mysql://root:@127.0.0.1/clothesshop',
                'dbold'=> 'mysql://root:@127.0.0.1/renzuiyixiang',
            ),
            'sphinx'=> array(  //全文索引
            ),
            //请求行为名
            'action' => 'a',
            'memcache'=> array(  //memcache配置
                'memcached'  => array(),
            ),

            'censor'=> array(  //观察者密钥
                'user'=> 'bookuu',
                'key'=>  '12345678',
            ),
        ),

    );

    /**
     * 构造
     *
     * @access public
     * @return boolean
     */
    function __construct()
    {
    	if (empty($this->_supports[self::Host()])) {
    		throw new BException('domain_unsupported', self::Host());
    	}
        return $this;
    }

    /**
     * 当前系统
     *
     * @access public
     * @return boolean
     */
    static function System()
    {
    	if (strpos(php_uname(), 'Windows') !== false) {
            return 'Windows';
        } else {
            return 'linux';
        }
    }

    /**
     * 当前主机名
     *
     * @access public
     * @param bool $reduce 是否简化
     * @return string
     */
    static function Host($reduce = true)
    {
        $domain = strtolower($_SERVER['HTTP_HOST']);
        if ($reduce && preg_match_all('/^(([\w\d]+)\.)?([\w\d]+)\.(\w+)(:\d+)?$/', $domain, $matches)) {
            return sprintf('%s.%s', $matches[3][0], $matches[4][0]);
        }
        return $domain;
    }

    /**
     * 当前请求
     */
    static function Action ()
    {
        if (strpos($_SERVER['PHP_SELF'], '?') !== false) {
            list($_SERVER['PHP_SELF'],) = explode('?', $_SERVER['PHP_SELF']);
        }
        $tmp = pathinfo($_SERVER['PHP_SELF']);
        return $tmp['filename'] ? $tmp['filename'] : 'index';
    }

    /**
     * 取值
     *
     * @access public
     * @param string $name
     * @return bool
     */
    function get ($name)
    {
    	return $this->_supports[self::Host()][$name];
    }

    /**
     * 设值
     *
     * @access public
     * @param string $name
     * @param mixed $value
     * @return bool
     */
    function set($name, $value)
    {
        $this->_supports[self::Host()][$name] = $value;
        return true;
    }

    /**
     * 取得当前域名下相关配置属性
     *
     * @access public
     * @param string $name
     * @return mixed
     */
    function __get($name)
    {
        return $this->get($name);
    }

    function __set($name, $value)
    {
        return $this->set($name, $value);
    }
}

?>
